﻿//新增
function add(pid) {
    win.open("/gx/sysmenu/addedit?pid=" + pid, 550, 450, "新增");
}
//编辑
function edit(id) {
    win.open("/gx/sysmenu/addedit?id=" + id, 550, 450, "编辑");
}
//删除
function del(id, obj) {
    win.confirm("确认要删除吗？", function () {
        $.post("/gx/sysmenu/deletedata", { id: id }, function (res) {
            if (res.Code == 200) {
                $(obj).parent().parent().parent().remove();
            } else {
                win.alert("删除失败");
            }
        });
    });
}
//父级展开
function onOpen(obj, id) {
    if ($(obj).siblings("i").hasClass("layui-icon-right")) {
        $(obj).siblings("i").removeClass("layui-icon-right").addClass("layui-icon-down");
        $("#nodeContent" + id).addClass("layui-show");
    } else {
        $(obj).siblings("i").removeClass("layui-icon-down").addClass("layui-icon-right");
        $("#nodeContent" + id).removeClass("layui-show");
    }
}
//子级展开
function onNodeOpen(obj, id) {
    if ($(obj).siblings("i").hasClass("layui-icon-right")) {
        $(obj).siblings("i").removeClass("layui-icon-right").addClass("layui-icon-down");
        $("#btnContent" + id).addClass("layui-show");
    } else {
        $(obj).siblings("i").removeClass("layui-icon-down").addClass("layui-icon-right");
        $("#btnContent" + id).removeClass("layui-show");
    }
}
//修改排序
function updateSort(id, obj) {
    $.post("/gx/sysmenu/updatesort", { id: id, sort: $(obj).val() }, function (res) { });
}
//获取菜单
function getModel(item, id) {
    if (id > 0) {
        $("#lbl" + id).html(item.Name);
        $("#lbl" + id).parent().find("input[name='sort']").val(item.Sort);
    } else {
        var menuHtml = "<div class=\"layui-colla-item\">";
        menuHtml += "<h2 class=\"layui-colla-title\">";
        menuHtml += "<i class=\"layui-icon layui-icon-right\"></i>&nbsp;&nbsp;";
        if (item.ParentId == 0) {
            menuHtml += "<label class=\"plbl\" onclick=\"onOpen(this," + item.Id + ")\">" + item.Name + "</label>";
            menuHtml += "<div class=\"layui-btn-group\" style=\"padding-left:10px;border-left:1px solid #ddd;\">";
            menuHtml += "<button class=\"layui-btn layui-btn-sm\" onclick=\"add(" + item.Id + ")\" title=\"新增\">";
            menuHtml += "<i class=\"layui-icon\">&#xe654;</i>";
            menuHtml += "</button>";
        } else {
            menuHtml += "<label onclick=\"onNodeOpen(this," + item.Id + ")\">" + item.Name + "</label>";
            menuHtml += "<div class=\"layui-btn-group\" style=\"padding-left:10px;border-left:1px solid #ddd;\">";
        }
        menuHtml += "<button class=\"layui-btn layui-btn-sm\" onclick=\"edit(" + item.Id + ")\" title=\"编辑\">";
        menuHtml += "<i class=\"layui-icon\">&#xe642;</i>";
        menuHtml += "</button>";
        menuHtml += "<button class=\"layui-btn layui-btn-sm\" onclick=\"del(" + item.Id + ",this)\" title=\"删除\">";
        menuHtml += "<i class=\"layui-icon\">&#xe640;</i>";
        menuHtml += "</button>";
        menuHtml += "</div>";
        menuHtml += "<div style=\"display:inline-block;width:80px;\">";
        menuHtml += "<input type=\"number\" placeholder=\"排序\" class=\"layui-input\" style=\"height:30px;\" value=\"" + item.Sort + "\" onchange=\"updateSort(" + item.Id + ",this)\" />";
        menuHtml += "</div>";
        menuHtml += "</h2>";
        menuHtml += "<div class=\"layui-colla-content\" id=\"" + (item.ParentId > 0 ? "btn" : "node") + "Content" + item.Id + "\">";
        if (item.ParentId > 0) {
            menuHtml += "<button class=\"layui-btn layui-btn-sm\" title=\"新增按钮\" onclick=\"addBtn(" + item.Id + ")\">";
            menuHtml += "<i class=\"layui-icon\">&#xe654;</i>";
            menuHtml += "</button>";
            menuHtml += "<table class=\"layui-table\">";
            menuHtml += "<colgroup><col width=\"214.5\"><col></colgroup>";
            menuHtml += "<tbody id=\"tbd" + item.Id + "\">";
            menuHtml += "</tbody>";
            menuHtml += "</table>";
        } else {
            menuHtml += "<div class=\"layui-collapse\">";
            menuHtml += "</div>";
        }
        menuHtml += "</div>";
        menuHtml += "</div>";
        if (item.ParentId > 0) {
            $("#nodeContent" + item.ParentId + " .layui-collapse").append(menuHtml);
        } else {
            window.location.reload();
        }
    }
}
/*******************************按钮******************************/
//新增
function addBtn(mid) {
    win.open("/gx/sysbutton/addedit?mid=" + mid, 550, 550, "新增");
}
//编辑
function editBtn(id) {
    win.open("/gx/sysbutton/addedit?id=" + id, 550, 550, "编辑");
}
//删除
function delBtn(id, obj) {
    win.confirm("确认要删除吗？", function () {
        $.post("/gx/sysbutton/deletedata", { id: id }, function (res) {
            if (res.Code == 200) {
                $(obj).parent().parent().parent().remove();
            } else {
                win.alert("删除失败");
            }
        });
    });
}
//修改排序
function updateBtnSort(id, obj) {
    $.post("/gx/sysbutton/updatesort", { id: id, sort: $(obj).val() }, function (res) { });
}
//获取菜单按钮
function getBtnModel(obj, id) {
     if (id>0) {
        $("#td" + id).html(obj.Name);
        $("#td" + id).parent().find("input[name='sort']").val(obj.Sort);
    } else {
        var btnHtml = "<tr>";
        btnHtml += "<td>" + obj.Name + "</td>";
        btnHtml += "<td>";
        btnHtml += "<div class=\"layui-btn-group\">";
        btnHtml += "<button class=\"layui-btn layui-btn-primary layui-btn-sm\" onclick=\"editBtn(" + obj.Id + ")\" title=\"编辑\">";
        btnHtml += "<i class=\"layui-icon\">&#xe642;</i>";
        btnHtml += "</button>";
        btnHtml += "<button class=\"layui-btn layui-btn-primary layui-btn-sm\" onclick=\"delBtn(" + obj.Id + ",this)\" title=\"删除\">";
        btnHtml += "<i class=\"layui-icon\">&#xe640;</i>";
        btnHtml += "</button>";
        btnHtml += "</div>";
        btnHtml += "<div style=\"display:inline-block;width:80px;\">";
        btnHtml += "<input type=\"number\" placeholder=\"排序\" class=\"layui-input\" style=\"height:30px;\" value=\"" + obj.Sort + "\" onchange=\"updateBtnSort(" + obj.Id + ",this)\" />";
        btnHtml += "</div>";
        btnHtml += "</td>";
        btnHtml += "</tr>";
        $("#tbd" + obj.FK_MenuId).append(btnHtml);
    }
}